package GUI.forms;

import i18n.JButtonI18N;
import i18n.JLabelI18N;
import i18n.LanguageUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import GUI.EnginesManager;
import GUI.GUI;
import GUI.common.ExceptionI18N;
import board.Game;
import engine.ChessEngine;

public class FEngines extends GeneralForm {
	private EnginesManager em=null;

	public FEngines(GUI gui) {
		super(gui);
		try {
			em= new EnginesManager(new File("./settings/engines"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		initComponents();
		loadEngines();
		
		// TODO Auto-generated constructor stub
	}
	
	public ChessEngine getSelectedEngine(){
    	int selectedRow = this.jTEngines.getSelectedRow();
    	return (selectedRow!=-1?this.em.getEngines().get(selectedRow):null);
	}
	
	
	
	public void deleteEngine(){
		try {
			this.em.deleteEngine(this.getSelectedEngine());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		loadEngines();
	}
	public void addEngine(File file){
		try {
			this.em.addEngine(file.getAbsolutePath());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		loadEngines();
	}

	@Override
	protected boolean validation() throws ExceptionI18N {
		// TODO Auto-generated method stub
		return false;
	}
	
	private void loadEngines(){
		Object[][] model=null; 
		int i =0;
		if(em.getEngines()!=null && em.getEngines().size()>0){
			model = new Object[em.getEngines().size()][1];
			for(ChessEngine engine:em.getEngines()){
				model[i][0]=engine.getName();								
				i++;
			}
		}		
		TableModel dataModel = new DefaultTableModel(model, new String [] {			        		    
				""
		});		
		this.jTEngines.setModel(dataModel);		
	}
	
	private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTEngines = new javax.swing.JTable();
        jBNou = new JButtonI18N("Form.Add",this, "cmd.AddEngine") ;
        jBModificar = new JButtonI18N("Form.Delete",this, "cmd.DeleteEngine") ;
        jLabel1 = new  JLabelI18N("Form.Engines.Title");

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTEngines.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTEngines.setName("jTEngines"); // NOI18N
        jScrollPane1.setViewportView(jTEngines);

        
        
        jBNou.setName("jBAdd"); // NOI18N

        
        jBModificar.setName("jBDelete"); // NOI18N

        
        jLabel1.setName("jLTitle"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 559, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 559, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBNou, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 112, Short.MAX_VALUE)
                        .addComponent(jBModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBNou)
                    .addComponent(jBModificar))
                .addContainerGap())
        );
    }// </editor-fold>


    // Variables declaration - do not modify
    private JButtonI18N jBModificar;
    private JButtonI18N jBNou;
    private JLabelI18N jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTEngines;


}
